﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.PlcBytesObject;
using Zocono.WCS.Domain.ProcessModuleDomain;

namespace Zocono.WCS.Domain.EquipmentProcessDomain.PlanarGeneralMessage
{
    /// <summary>
    /// 平面设备通用PLC动作报文信息服务
    /// </summary>
    public class PlanarGeneralPlcActionBOFxMessageService : IPlanarActionMessageService
    {
        private readonly IPlanarProcessServiceMapInfoRepository _planarProcessServiceMapInfoRepository;
        private readonly IPlanarGeneralPlcActionProcessServiceFactory _planarGeneralPlcActionProcessServiceFactory;
        private readonly ILogUtil _logUtil;
        private readonly ISnowflake _Snowflake;
        private readonly ITelegramRecordInfoRepository _TelegramRecordInfoRepository;
        public PlanarGeneralPlcActionBOFxMessageService(IPlanarProcessServiceMapInfoRepository planarProcessServiceMapInfoRepository, IPlanarGeneralPlcActionProcessServiceFactory planarGeneralPlcActionProcessServiceFactory, ILogUtil logUtil, ISnowflake snowflake, ITelegramRecordInfoRepository telegramRecordInfoRepository)
        {
            _planarProcessServiceMapInfoRepository = planarProcessServiceMapInfoRepository;
            _planarGeneralPlcActionProcessServiceFactory = planarGeneralPlcActionProcessServiceFactory;
            _logUtil = logUtil;
            _Snowflake = snowflake;
            _TelegramRecordInfoRepository = telegramRecordInfoRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="plcActionMessage">plc动作报文</param>
        /// <param name="planarEquipmentDbObject">平面报文</param>
        /// <param name="plcStateMassge"></param>
        /// <returns></returns>
        public async Task ProcessActionMessageAsync(BytesObjectBase plcActionMessage, EquipmentDbObject planarEquipmentDbObject/*, BytesObjectBase plcStateMassge*/)
        {
            var plcMessage = plcActionMessage as PlanarGeneralPlcActionBOFx;
            var wcsMessage = planarEquipmentDbObject.bytesObjectBases.Find(o => o.ObjectType == "PlanarGeneralPlcActionBOFx") as PlanarGeneralPlcActionBOFx;
            if (wcsMessage == null)
            {
                return;
            }
 
            string equipmentCode = plcMessage.EquipmentCode;
            
 
            //TODO需要注入一个分选交互服务

            ////查询报文对应的处理类配置数据---需要添加判断PU和DO判断任务类型和状态
            //var serviceMapInfo = _planarProcessServiceMapInfoRepository.GetFirst(p => p.EquipmentCode == equipmentCode && 
            //p.PlcActionType == plcActionType && p.WcsActionType == wcsActionType);//通过设备码，PLC写信号，WCS写信好获取业务类
            //if (serviceMapInfo == null)
            //{
            //    _logUtil.Error($"普通线体设备：设备号({equipmentCode})查找平面设备业务配置PLC握手【{plcActionType}】-WCS握手【{wcsActionType}】未配置！");
            //    return;
            //}
            ////按配置信息创建平面正常报文业务处理类 
            //var processService = _planarGeneralPlcActionProcessServiceFactory.Create(serviceMapInfo.PlanarProcessServiceName);//根据信号运行相应类
            //if (processService == null)
            //{
            //    _logUtil.Error($"普通线体设备：设备号({equipmentCode})创建服务({serviceMapInfo.PlanarProcessServiceName})异常:at PlanarGeneralPlcActionBOMessageService.ProcessActionMessageAsync");
            //    return;
            //}
            ////处理对应业务
            //await processService.ProcessActionMessageAsync(plcMessage, wcsMessage, planarEquipmentDbObject/*, plcStateMassge*/);
        }
    }
}
